package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.util.List;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RestController
public class McpClientController {
    @Qualifier("chatClient")
    @Autowired
    private ChatClient chatClient;

    @Autowired
    private List<ToolCallbackProvider> tcbProviders;


    @GetMapping("/mcp")
    public Flux<String> mcp(@RequestParam(value = "message", defaultValue = "1+1=?") String message) {
        return Flux.<String>create(
                sink -> {
                    chatClient.prompt()
                            .toolCallbacks(tcbProviders.toArray(new ToolCallbackProvider[0]))
                            .user(message)
                            .stream()
                            .content()
                            .doAfterTerminate(()-> sink.complete())
                            .subscribe(t -> sink.next(t));
                }
        ).subscribeOn(Schedulers.boundedElastic());
    }
}
